<?php
namespace app\admin\controller;
use app\common\model\Question as QuestionModel;
use app\common\model\Pk as PkModel;

class Customer extends Base
{   
  /* 常见问题列表 */
  public function questionList () {
    $where = array();
    $page = max(input('page'),1);
    $size = 20;
    $list = QuestionModel::getPage($where,$page,$size,'ord desc');
    $total = QuestionModel::getCount($where);
    if(!$list) {
      return returnJson(0,'',[
        'list' => [],
        'size' => $size,
        'total' => $total
      ]);
    }
    return returnJson(0,'',[
        'list' => $list,
        'size' => $size,
        'total' => $total
    ]);
  }

  /*  问题编辑 */
  public function questionEdit () {
    if(request()->isPost()) {
      $input = input();
      if(!isset($input['title']) || !isset($input['content'])) {
          return returnJson(1,'请填写完整');
      }
      $data = array(
          'title' => $input['title'],
          'content' => $input['content'],
          'ord' => $input['ord']
      );
      if($input['id']) {
          $data['id'] = $input['id'];
      }
      if($input['id']) {
        $res = QuestionModel::where('id',$data['id'])->update($data);
      }else{
        $res = QuestionModel::create($data);
      }
      if($res) {
          return returnJson(0,'操作成功');
      }
      return returnJson(1,'网络异常，请重试');
    }
  }

  /* 问题删除 */
  public function questionDelete () {
    $input = input();
    if(!$input['id']) {
        return returnJson(1,'error');
    }
    $question = QuestionModel::getOne(['id'=>$input['id']]);
    if(!$question) {
        return returnJson(1,'问题不存在');
    }
    $res = QuestionModel::where(['id'=>$input['id']])->delete();
    if($res) {
        return returnJson(0,'已删除');
    }else{
        return returnJson(1,'删除失败，请重试');
    }
  }

  /* Pk列表 */
  public function pkList () {
    $where = array();
    $page = max(input('page'),1);
    $size = 20;
    $list = PkModel::getPage($where,$page,$size,'status desc,id desc');
    $total = PkModel::getCount($where);
    if(!$list) {
      return returnJson(0,'',[
        'list' => [],
        'size' => $size,
        'total' => $total
      ]);
    }
    return returnJson(0,'',[
        'list' => $list,
        'size' => $size,
        'total' => $total
    ]);
  }

  /*  Pk编辑 */
  public function pkEdit () {
    if(request()->isPost()) {
      $input = input();
      if(!isset($input['title']) || !isset($input['content'])) {
          return returnJson(1,'请填写完整');
      }
      $data = array(
          'title' => $input['title'],
          'content' => $input['content'],
          'status' => $input['status'],
          'start_time' => $input['start_time'],
          'end_time' => $input['end_time'],
          'sale_money' => $input['sale_money'],
          'prize_money' => $input['prize_money'],
          'prize_bili' => $input['prize_bili'],
          'sale_money_all' => $input['sale_money_all'],
      );
      if($data['status']==1) {
        PkModel::where('status',1)->update([
          'status' => 0
        ]);
      }
      if($input['id']) {
          $data['id'] = $input['id'];
      }
      if($input['id']) {
        $res = PkModel::where('id',$data['id'])->update($data);
      }else{
        $res = PkModel::create($data);
      }
      if($res) {
          return returnJson(0,'操作成功');
      }
      return returnJson(1,'网络异常，请重试');
    }
  }

  /* Pk删除 */
  public function pkDelete () {
    $input = input();
    if(!$input['id']) {
        return returnJson(1,'error');
    }
    $pk = PkModel::getOne(['id'=>$input['id']]);
    if(!$pk) {
        return returnJson(1,'Pk不存在');
    }
    $res = PkModel::where(['id'=>$input['id']])->delete();
    if($res) {
        return returnJson(0,'已删除');
    }else{
        return returnJson(1,'删除失败，请重试');
    }
  }
  
}

